package edu.gyc.hiseckill.service.impl;

import edu.gyc.hiseckill.model.Promo;
import edu.gyc.hiseckill.dao.PromoDao;
import edu.gyc.hiseckill.service.PromoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-01-30
 */
@Service
public class PromoServiceImpl extends ServiceImpl<PromoDao, Promo> implements PromoService {

    @Override
    public Promo getPromoByItemId(Integer id) {
        Promo promo=lambdaQuery().eq(Promo::getItemId,id).one();
        if (promo == null) { //不是每一个商品都是秒杀商品
            return null;
        }

        //获取当前时间是否在秒杀截止时间之前
        LocalDateTime now=LocalDateTime.now();

        if (now.isBefore(promo.getStartDate())) {
            promo.setStatus(1);
        } else if(now.isAfter(promo.getEndDate())){
            promo.setStatus(3);
        }else {
            promo.setStatus(2);
        }

        return promo;
    }
}
